//
//  CommunityViewController.m
//  Products_B
//
//  Created by lanou on 16/5/27.
//  Copyright © 2016年 朋学友. All rights reserved.
//

#import "CommunityViewController.h"
#import "CmtAttentionTableViewController.h"
#import "CmtRECTableViewController.h"
#import "CmtCollectionViewController.h"
#import "CmtActivityViewController.h"
#import "CmtShenQuViewController.h"
#import "CmtSecMoreLikeViewController.h"

#import "CmtProductDetailViewController.h"
#define kCmtAttentionUrl @"http://api.izhangchu.com/?=page=1&methodName=ShequFollow&size=20&version=4.40&"
@interface CommunityViewController ()<UIScrollViewDelegate>
@property(nonatomic,strong)UIView *v1;
@property(nonatomic,strong)UIView *v2;
@property(nonatomic,strong)UIView *v3;

@property(nonatomic,strong)CmtAttentionTableViewController *CmtTableVc;

@property(nonatomic,strong)UIScrollView *mainScrollView;

@property(nonatomic,strong)CmtRECTableViewController *cmtRecTableVc;

@property(nonatomic,strong)CmtCollectionViewController *CollectionVc;
@property(nonatomic,strong)UIView *downView;
@end

@implementation CommunityViewController

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = NO;
    
}

-(void)viewWillDisappear:(BOOL)animated{

    self.navigationController.navigationBarHidden = YES;
}


-(void)viewWillLayoutSubviews{
    
    [super viewWillLayoutSubviews];
    self.navigationController.navigationBar.translucent = NO;
    
    //    barButton的背景图片
    UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(100,0, kWidth - 120, 40)] ;
    //    建barButton
    UIBarButtonItem *barBtn = [[UIBarButtonItem alloc] initWithCustomView:customView];
    self.navigationItem.leftBarButtonItems = @[barBtn];
    
    //    NSLog(@"self.navigation = %@",[self.navigationController class]);
    
    //    关注button
    UIButton *attentionBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [attentionBtn addTarget:self action:@selector(attentionBtn) forControlEvents:UIControlEventTouchUpInside];
    [attentionBtn setTitle:@"关注" forState:(UIControlStateNormal)];
    [attentionBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    attentionBtn.frame = CGRectMake(100, 3, 50, 32);
    [customView addSubview:attentionBtn];
    
    self.v1 = [[UIView alloc]initWithFrame:CGRectMake(100, 34, 50, 5)];
//    self.v1.backgroundColor = [UIColor whiteColor];
    [customView addSubview:_v1];
    
    
    //    推荐button
    UIButton *recommendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [recommendBtn setTitle:@"推荐" forState:(UIControlStateNormal)];
    [recommendBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [recommendBtn addTarget:self action:@selector(recommendBtn) forControlEvents:UIControlEventTouchUpInside];
    recommendBtn.frame = CGRectMake(160, 3, 50, 32);
    [customView addSubview:recommendBtn];
    self.v2 = [[UIView alloc]initWithFrame:CGRectMake(160, 34, 50, 5)];
//    self.v2.backgroundColor = [UIColor orangeColor];
    [customView addSubview:_v2];
    
    
    //    最新按钮
    UIButton *newestBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [newestBtn setTitle:@"最新" forState:(UIControlStateNormal)];
    [newestBtn addTarget:self action:@selector(newestBtn) forControlEvents:UIControlEventTouchUpInside];
    [newestBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    newestBtn.frame = CGRectMake(220, 3, 50, 32);
    [customView addSubview:newestBtn];
    
    self.v3 = [[UIView alloc]initWithFrame:CGRectMake(220, 34, 50, 5)];
//    self.v3.backgroundColor = [UIColor whiteColor];
    [customView addSubview:_v3];
    
    //    活动按钮
    UIBarButtonItem *rigBarButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"activity.png"] style:(UIBarButtonItemStylePlain) target:self action:@selector(ActivityAction:)];
    self.navigationItem.rightBarButtonItem = rigBarButton;
    
   
    
    if (_mainScrollView.contentOffset.x / kWidth == 0) {
     
        [self attentionBtn];
    }
    if (_mainScrollView.contentOffset.x / kWidth == 1) {
        [self recommendBtn];
    }
    if (_mainScrollView.contentOffset.x / kWidth == 2) {
        [self newestBtn];
    }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
//    主控制器
    _mainScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 64 - 50)];
    _mainScrollView.contentSize = CGSizeMake(kWidth * 3, kHeight - 64 - 50);
    _mainScrollView.delegate = self;
    _mainScrollView.showsHorizontalScrollIndicator = NO;
    _mainScrollView.showsVerticalScrollIndicator = NO;
    _mainScrollView.pagingEnabled = YES;
    _mainScrollView.contentOffset = CGPointMake(kWidth, 0);
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:_mainScrollView];
    _mainScrollView.bounces = NO;
    
    
//    关注控制器
    
    _CmtTableVc = [[CmtAttentionTableViewController alloc]init];
//    UINavigationController *CmtTableNav = [[UINavigationController alloc]initWithRootViewController:_CmtTableVc];
    _CmtTableVc.tableView.frame = CGRectMake(0, 0, kWidth, kHeight - 64 - 50);
    _CmtTableVc.tableView.backgroundColor = [UIColor redColor];
    [_mainScrollView addSubview:_CmtTableVc.view];
    
    __block CommunityViewController *blockSelf = self;
    _CmtTableVc.block = ^(NSString *post_id){
        CmtProductDetailViewController *CmtProductVc = [[CmtProductDetailViewController alloc]init];
        CmtProductVc.post_id = post_id;
        [blockSelf.navigationController pushViewController:CmtProductVc animated:YES];
    };
    _CmtTableVc.user_idBlock = ^(NSString *user_id){
    
        CmtShenQuViewController *CmtSQVc = [[CmtShenQuViewController alloc]init];
        CmtSQVc.user_id = user_id;
        [blockSelf.navigationController pushViewController:CmtSQVc animated:YES];
    };
    _CmtTableVc.post_idBlock = ^(NSString *post_id){
        CmtSecMoreLikeViewController *CmtMLVc = [[CmtSecMoreLikeViewController alloc]init];
        CmtMLVc.post_id = post_id;
        [blockSelf.navigationController pushViewController:CmtMLVc animated:YES];
    };
    _CmtTableVc.CmtAttenBlock = ^(BOOL isHidden){
        
        blockSelf.downView.hidden = isHidden;
    };
    

    
//    [self addChildViewController:CmtTableNav];
    
//    推荐控制器
    self.cmtRecTableVc = [[CmtRECTableViewController alloc]init];
     _cmtRecTableVc.tableView.frame = CGRectMake(kWidth, 0, kWidth, kHeight - 64 - 50);
    _cmtRecTableVc.tableView.backgroundColor = [UIColor cyanColor];
    [_mainScrollView addSubview:_cmtRecTableVc.tableView];
    [self addChildViewController:_cmtRecTableVc];
    _cmtRecTableVc.block = ^(BOOL isHidden){
    
        blockSelf.downView.hidden = isHidden;
    };
    
//    第三个界面
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.CollectionVc = [[CmtCollectionViewController alloc]initWithCollectionViewLayout:flowLayout];
    self.CollectionVc.collectionView.backgroundColor = [UIColor whiteColor];
    self.CollectionVc.collectionView.frame = CGRectMake(kWidth * 2, 0, kWidth, kHeight - 64 - 50);
    [_mainScrollView addSubview:self.CollectionVc.collectionView ];
    [self addChildViewController:_CollectionVc];
    
    _CollectionVc.block = ^(BOOL isHidden){
    
        blockSelf.downView.hidden = isHidden;
    };
    
    
    [self initDownView];
}

//向下滑的弹窗
-(void)initDownView{
    
    self.downView = [[UIView alloc]initWithFrame:CGRectMake(kWidth * _mainScrollView.contentOffset.y / kWidth,kHeight - 160, kWidth, 50)];
//    self.downView.backgroundColor = [UIColor redColor];
    self.downView.backgroundColor = PKCOLOR(251, 241, 231);
    [_mainScrollView addSubview:_downView];
    self.downView.hidden = YES;
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    button.frame = CGRectMake((kWidth - 40) / 2, 0, 40, 40);
    [button setImage:[UIImage imageNamed:@"CmtCamera.png"] forState:(UIControlStateNormal)];
    [button addTarget:self action:@selector(CameraAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.downView addSubview:button];
}


-(void)CameraAction:(UIButton *)button{
    NSLog(@"点击了按钮");
}


//跳转到活动界面
-(void)ActivityAction:(UIBarButtonItem *)barButton{
    CmtActivityViewController *cmtActVc = [[CmtActivityViewController alloc]init];
    [self.navigationController pushViewController:cmtActVc animated:YES];
}

//推荐
-(void)recommendBtn{
    self.v1.backgroundColor = [UIColor whiteColor];
    self.v2.backgroundColor = [UIColor orangeColor];
    self.v3.backgroundColor = [UIColor whiteColor];
    
    [self.mainScrollView setContentOffset:CGPointMake(kWidth, 0) animated:NO];
    self.downView.frame = CGRectMake(kWidth, kHeight - 160, kWidth, 50);
}
//关注
-(void)attentionBtn{

    self.v1.backgroundColor = [UIColor orangeColor];
    self.v2.backgroundColor = [UIColor whiteColor];
    self.v3.backgroundColor = [UIColor whiteColor];
    [self.mainScrollView setContentOffset:CGPointMake(0, 0) animated:NO];
    self.downView.frame = CGRectMake(0, kHeight - 160, kWidth, 50);
}
//最新
-(void)newestBtn{
    
    self.v1.backgroundColor = [UIColor whiteColor];
    self.v2.backgroundColor = [UIColor whiteColor];
    self.v3.backgroundColor = [UIColor orangeColor];
   [self.mainScrollView setContentOffset:CGPointMake(kWidth * 2, 0) animated:NO];
    self.downView.frame = CGRectMake(kWidth * 2, kHeight - 160, kWidth, 50);
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    if ([scrollView isEqual:[_mainScrollView class]]) {
        return;
    }
    
    if (scrollView.contentOffset.x / kWidth == 0) {

        [self attentionBtn];
        self.downView.frame = CGRectMake(kWidth * 0, kHeight - 160, kWidth, 50);
        
    }else if (scrollView.contentOffset.x / kWidth == 1){
        
        [self recommendBtn];
        self.downView.frame = CGRectMake(kWidth, kHeight - 160, kWidth, 50);
        
    }else{

        [self newestBtn];
        self.downView.frame = CGRectMake(kWidth * 2, kHeight - 160, kWidth, 50);
    }
    
}

//隐藏tabBar（UItableViewController）
- (id)init {
    self = [super init];
    if (self) {
        self.hidesBottomBarWhenPushed = NO;
    }
    return self;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
